# -*- coding: utf-8 -*-
# created on 2016/10/8

from mathsolver.functions.zhixian.base import ZhiXian
from mathsolver.functions.base import *
from sympy.abc import a, b, x, y
from sympy import S, solve, simplify


class PointUpdate(BaseFunction):
    def solver(self, *args):
        if isinstance(args[0], BasePoint):
            for arg in args:
                self.output.append(arg)
        elif isinstance(args[0], BasePoints):
            self.output.extend([BasePoint(value) for value in args[0].value])
        elif len(args) == 2:
            point_v = args[1].value
            self.output.append(BasePoint({"name": args[0].value, "value": point_v}))
        return self


class LineInitFake(BaseFunction):
    def solver(self, *args):
        name = args[0].value
        zhixian = BaseZhiXian({"name": name, "value": ["a*x + y + 1", "0"]})
        # eq = dairu_fangcheng001().solver(BasePoint({"name":"", "value":[2,3]}), zhixian.txt).output[0]
        self.output.append(zhixian)
        return self


class LineInitPoint(BaseFunction):
    def solver(self, *args):
        name = args[0].value
        point = args[1].sympify()
        eq = [a * (x - point[0]) + b * (y - point[1]), S.Zero]
        zhixian = BaseZhiXian({"name": name, "value": eq})
        self.steps.append(["设过%s的直线方程为" %
                           (BasePoint({"name": "", "value": point}).printing()), zhixian.printing()])
        self.output.append(zhixian)
        self.label.add("过点设直线的点法向式方程")
        return self


class LineInitPingXing(BaseFunction):
    def solver(self, *args):
        name = args[0].value
        zhixian0 = self.search(name)
        zhixian0 = zhixian0.sympify()
        zhixian1 = args[1].sympify()
        symbol = list((zhixian0[0] - zhixian0[1]).free_symbols.difference([x, y]))[0]
        target_symbol = list((zhixian0[0] - zhixian0[1]).free_symbols.difference([x, y]))[1]
        zhixian0_fa_vector = ZhiXian(BaseEq(zhixian0)).normal_Vector.sympify()
        zhixian1_fa_vector = ZhiXian(BaseEq(zhixian1)).normal_Vector.sympify()
        self.steps.append(["", "∵%s = %s 和 %s = %s 平行" % (new_latex(zhixian0[0]), new_latex(zhixian0[1]), new_latex(zhixian1[0]), new_latex(zhixian1[1]))])
        self.steps.append(["", "(%s) × (%s) = (%s) × (%s)" % (new_latex(zhixian0_fa_vector[0]), new_latex(zhixian1_fa_vector[1]), new_latex(zhixian0_fa_vector[1]), new_latex(zhixian1_fa_vector[0]))])
        eq = zhixian0_fa_vector[0] * zhixian1_fa_vector[1] - zhixian0_fa_vector[1] * zhixian1_fa_vector[0]
        answer = solve(eq, symbol)
        self.steps.append(["", "解得：%s = %s" % (new_latex(symbol), new_latex(answer[0]))])
        new_zhixian_left = simplify(zhixian0[0].subs({symbol: answer[0]}) / target_symbol)
        new_zhixian_right = simplify(zhixian0[1].subs({symbol: answer[0]}) / target_symbol)
        new_zhixian0 = [new_zhixian_left, new_zhixian_right]
        self.steps.append(["将%s = %s代入方程，化简得" % (new_latex(symbol), new_latex(answer[0])),
                           BaseZhiXian({"name": name, "value": new_zhixian0}).printing()])
        self.output.append(BaseZhiXian({"name": name, "value": new_zhixian0}))

        self.label.add("过直线外一点求该直线的平行线方程")
        return self


class LineInitChuiZhi(BaseFunction):
    def solver(self, *args):
        name = args[0].value
        zhixian0 = self.search(name)
        zhixian0 = zhixian0.sympify()
        zhixian1 = args[1].sympify()
        symbol = list((zhixian0[0] - zhixian0[1]).free_symbols.difference([x, y]))[0]
        target_symbol = list((zhixian0[0] - zhixian0[1]).free_symbols.difference([x, y]))[1]
        zhixian0_fa_vector = ZhiXian(BaseEq(zhixian0)).normal_Vector.sympify()
        zhixian1_fa_vector = ZhiXian(BaseEq(zhixian1)).normal_Vector.sympify()
        self.steps.append(["", "∵%s = %s 和 %s = %s 垂直" % (new_latex(zhixian0[0]), new_latex(zhixian0[1]), new_latex(zhixian1[0]), new_latex(zhixian1[1]))])
        self.steps.append(["", "(%s) × (%s) + (%s) × (%s) = 0" % (new_latex(zhixian0_fa_vector[0]), new_latex(zhixian1_fa_vector[0]), new_latex(zhixian0_fa_vector[1]), new_latex(zhixian1_fa_vector[1]))])
        eq = zhixian0_fa_vector[0] * zhixian1_fa_vector[0] + zhixian0_fa_vector[1] * zhixian1_fa_vector[1]
        eq_symbols = list(eq.free_symbols)
        if len(eq_symbols) == 1:
            symbol = eq_symbols[0]
        answer = solve(eq, symbol)
        self.steps.append(["", "解得：%s = %s" % (new_latex(symbol), new_latex(answer[0]))])
        if len(eq_symbols) == 1:
            new_zhixian_left = zhixian0[0].subs({symbol: answer[0]})
            new_zhixian_right = zhixian0[1].subs({symbol: answer[0]})
            expr = new_zhixian_left - new_zhixian_right
            diff_expr_symbol = list(expr.free_symbols.difference([x, y]))[0]
            new_zhixian_left = simplify(new_zhixian_left / diff_expr_symbol)
            new_zhixian_right = simplify(new_zhixian_right / diff_expr_symbol)
            new_zhixian0 = [new_zhixian_left, new_zhixian_right]
        else:
            new_zhixian_left = simplify(zhixian0[0].subs({symbol: answer[0]}) / target_symbol)
            new_zhixian_right = simplify(zhixian0[1].subs({symbol: answer[0]}) / target_symbol)
            new_zhixian0 = [new_zhixian_left, new_zhixian_right]
        self.steps.append(["将%s = %s代入方程，化简得" % (new_latex(symbol), new_latex(answer[0])),
                           BaseZhiXian({"name": name, "value": new_zhixian0}).printing()])
        self.output.append(BaseZhiXian({"name": name, "value": new_zhixian0}))

        self.label.add("过直线外一点求该直线的垂线方程")
        return self


class LineInitXieLv(BaseFunction):
    def solver(self, *args):
        name = args[0].value
        zhixian0 = self.search(name)
        zhixian0 = zhixian0.sympify()
        xielv = args[1].sympify()
        slope = ZhiXian(BaseEq(zhixian0)).slope.sympify()
        self.steps.append(["∵直线的斜率为%s，则" % (new_latex(xielv)), BaseEq([xielv, slope]).printing()])
        eq = slope - xielv
        symbol = list(slope.free_symbols)[0]
        target_symbol = list(slope.free_symbols)[1]
        answer = solve(eq, symbol)
        self.steps.append(["", "解得：%s = %s" % (new_latex(symbol), new_latex(answer[0]))])
        new_zhixian_left = simplify(zhixian0[0].subs({symbol: answer[0]}) / target_symbol)
        new_zhixian_right = simplify(zhixian0[1].subs({symbol: answer[0]}) / target_symbol)
        new_zhixian0 = [new_zhixian_left, new_zhixian_right]
        self.steps.append(["将%s = %s代入方程，化简得" % (new_latex(symbol), new_latex(answer[0])),
                           BaseZhiXian({"name": name, "value": new_zhixian0}).printing()])
        self.output.append(BaseZhiXian({"name": name, "value": new_zhixian0}))
        self.label.add("求直线方程：点斜式")
        return self


class LineInitXieLv002(BaseFunction):
    def solver(self, *args):
        name = args[0].value
        xielv = args[1].sympify()
        eq = [y, xielv * x + b]
        self.output.append(BaseZhiXian({"name": name, "value": eq}))
        self.label.add("设直线方程：点斜式")
        return self


class ReturnLineFangCheng(BaseFunction):
    """
    直线l的方程是y=x-1,则该直线l的倾斜角为()
    """
    def solver(self, *args):
        line_name = args[0].value
        eq = args[1].sympify()
        self.output.append(BaseZhiXian({"name": line_name, "value": eq}))
        return self


if __name__ == '__main__':
    pass
